package com.oa.service.user.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.oa.mapper.user.DepartmentMapper;
import com.oa.mapper.user.PositionMapper;
import com.oa.mapper.user.UserMapper;
import com.oa.pojo.Department;
import com.oa.pojo.Emp;
import com.oa.pojo.Position;
import com.oa.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.util.List;

/**
 * @Author: 一拳超人
 * @Date: 2021/10/2 16:09
 */
@Service
public class UserImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private DepartmentMapper departmentMapper;

    @Autowired
    private PositionMapper positionMapper;

    /**
     * 员工登录验证
     *
     * @param emp 员工信息
     * @return 员工信息
     */
    @Override
    public Emp login(Emp emp) {
        //加密
        String password = DigestUtils.md5DigestAsHex(emp.getEmpPassword().getBytes());
        return userMapper.selectOne(new QueryWrapper<>(emp.setEmpPassword(password)));
    }

    /**
     * 前台员工信息搜索
     *
     * @param name 员工姓名
     * @return 搜索到的员工列表
     */
    @Override
    public List<Emp> selectEmpInfo(String name) {
        List<Emp> empList = userMapper.selectList(new QueryWrapper<Emp>().like("emp_name", name));
        for (Emp emp : empList) {
            emp.setDepName(
                    departmentMapper.selectOne(new QueryWrapper<Department>()
                            .eq("dep_id", emp.getDepId())).getDepName())
                    .setPositionName(positionMapper.selectOne(new QueryWrapper<Position>()
                            .eq("position_id", emp.getPositionId())).getPositionName());
        }
        return empList;
    }

    /**
     * 员工个人信息查询
     *
     * @param empId 员工编号
     * @return 员工个人信息
     */
    @Override
    public Emp selectOneself(String empId) {
        Emp emp = userMapper.selectOne(new QueryWrapper<>(new Emp().setEmpId(empId)));
        emp.setPositionName(positionMapper.selectOne(new QueryWrapper<Position>()
                .eq("position_id", emp.getPositionId())).getPositionName())
                .setDepName(departmentMapper.selectOne(new QueryWrapper<Department>()
                        .eq("dep_id", emp.getDepId())).getDepName());
        return emp;
    }

    /**
     * 员工个人信息修改
     *
     * @param emp 需要修改的个人信息
     * @return 修改结果
     */
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public String updateOneselfInfo(Emp emp) {
        int i = userMapper.updateById(emp);
        if (i != 0) {
            return "修改成功";
        }
        return "修改失败";
    }

    @Override
    public Integer changeOneselfPassword(Emp emp) {
        String password = DigestUtils.md5DigestAsHex(emp.getEmpPassword().getBytes());
        QueryWrapper<Emp> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("emp_id", emp.getEmpId());
        System.out.println(emp.setEmpPassword(password));
        int i = userMapper.update(emp, queryWrapper);
        return i != 0 ? 200 : 201;
    }
}

